package com.sinodata.bsm.cicp.collector.ha;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CollectorUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 双机--> 节点--> 接口 的 采集程序，采集 接口地址和接口装填
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 9:57:19    liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class HAInterfaceCollector extends Collector {

    private static final Logger logger = Logger.getLogger(HAInterfaceCollector.class);

    /**
     * 采集方法
     * @param ip 执行命令的主机ip地址
     * @return 资源列表
     * @throws IOException
     */
    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> resProValList = null;
        BufferedReader reader = null;
        try {
            reader = CollectorUtil.getReader("clstat -o", ip);
            // 调用解析程序
            resProValList = this.parseReader(reader);
        } catch (IOException e) {
            logger.error("Failed to collector HAInterface ", e);
            throw new CollectorException("Failed to collector HAInterface ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("Failed to close IO resource ", e);
                    throw new CollectorException("Failed to close IO Resource ", e);
                }
            }
        }
        return resProValList;
    }

    /**
     * 解析 响应结果
     * 
     * @param reader 响应结果
     * @return 资源列表
     * @throws IOException
     */
    private List<ResPropertyValue> parseReader(BufferedReader reader) throws IOException {
        List<ResPropertyValue> resProValList = null;
        String typeStr = this.getParamMap().get(getParamMeta()[0]);
        // 匹配（该行数据的 Interface 的值 是用于判断的条件）: Interface : xxx Address : xxxx
        Pattern codePatn = Pattern.compile("\\s*Interface\\s*:\\s*([^\\(]*).*Address\\s*:\\s*(\\S*)\\s*");
        // 匹配（目标数据）: State :
        Pattern statPatn = Pattern.compile("\\s*State\\s*:\\s*(\\S+)\\s*");
        String[] propMetas = getPropMeta();
        String tmp = null;
        Matcher codeMach = null;
        Matcher statMach = null;
        ResPropertyValue propertyValueX = null;
        ResPropertyValue.PropValue[] propValues = null;
        while ((tmp = reader.readLine()) != null) {
            codeMach = codePatn.matcher(tmp);
            if (codeMach.matches()) {
                if (typeStr.equals(codeMach.group(1))) {
                    resProValList = new ArrayList<ResPropertyValue>();
                    propertyValueX = new ResPropertyValue();
                    propValues = new ResPropertyValue.PropValue[propMetas.length];
                    propValues[0] = ResPropertyValue.createPropValue(codeMach.group(2), propMetas[0]);

                    tmp = reader.readLine();
                    statMach = statPatn.matcher(tmp);
                    statMach.matches();
                    propValues[1] = ResPropertyValue.createPropValue(statMach.group(1), propMetas[1]);
                    propertyValueX.setValues(propValues);
                    resProValList.add(propertyValueX);
                    break;
                }
            }
        }
        return resProValList;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "Interface Name" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "Address", "Interface State" };
    }

}
